<template>
  <yt-page title="通讯联络" :rightData="rightData">
    <yt-group ref="group" :searchApi="searchApi" :group="false">
      <yt-cell slot-scope="user" labelWidth="65%">
        <div slot="label" class="flex">
          <span style="min-width: 50px">{{user.name}}</span>
          <span class="badge bg-gray" v-show="user.post">{{user.post}}</span>
        </div>
        <yt-cell-call :tel="user.mobile || ''"></yt-cell-call>
      </yt-cell>
    </yt-group>
    <yt-popup pos="right" v-model="search" :mask="true" slot="alert">
      <yt-page class="page" :flex="true" :leftData="[]" title="人员类别">
        <yt-form>
          <yt-inlineSelect v-model="categoryDicSelected" :list="categoryDic" label="D_text" val="D_code" :x="2"></yt-inlineSelect>
          <yt-btn-group slot="btn" :shadow="false">
            <yt-btn theme="white" @click="search = false">关闭</yt-btn>
            <yt-btn @click="onSearch">确定</yt-btn>
          </yt-btn-group>
        </yt-form>
      </yt-page>
    </yt-popup>
  </yt-page>
</template>

<script>
import { commonApi } from '@/common/api/commonApi.js'
import { ReplaceDictionary } from '@/common/utils/index.js'
import _ from 'lodash'

const CATEGORY = [
  {
    D_code: '1',
    D_text: '应急指挥部'
  },
  {
    D_code: '2',
    D_text: '应急队伍'
  },
  {
    D_code: '3',
    D_text: '应急专家'
  },
  {
    D_code: '4',
    D_text: '医疗机构'
  },
  {
    D_code: '5',
    D_text: '安管人员'
  },
  {
    D_code: '6',
    D_text: '特殊作业人员'
  },
  {
    D_code: '7',
    D_text: '重大危险源负责人'
  },
  {
    D_code: '8',
    D_text: '企业主要负责人'
  },
  {
    D_code: '9',
    D_text: '企业安全负责人'
  },
  {
    D_code: '10',
    D_text: '企业安全管理人员'
  },
  {
    D_code: '11',
    D_text: '视频管理人员'
  },
  {
    D_code: '12',
    D_text: '设备管理人员'
  }
]
export default {
  data() {
    return {
      input: false,
      inputValue: '',
      rightData: [
        {
          font: 'yt-icon yt-icon-search',
          click: () => {
            this.search = !this.search
          }
        }
      ],
      firstData: true,
      active: [],
      items: [],
      categoryDic: CATEGORY,
      categoryDicSelected: CATEGORY[0].D_code,
      search: false,
      searchApi: async ({ skip, limit }) => {
        let curCategory = CATEGORY.filter(
          v => v.D_code === this.categoryDicSelected
        )
        let items = []
        switch (curCategory[0].D_text) {
          case '应急指挥部':
            items = await commonApi.search('yj_mail_list_zh', { skip, limit })
            return items.map(v => {
              return {
                name: v.name,
                post: v.post,
                mobile: v.mobile
              }
            })
          case '应急队伍':
            items = await commonApi.search('yj_mail_list_dw', { skip, limit })
            return items.map(v => {
              return {
                name: v.department_people,
                post: v.ranks_name,
                mobile: v.department_mobile
              }
            })
          case '应急专家':
            items = await commonApi.search('yj_mail_list_zj', { skip, limit })
            return items.map(v => {
              return {
                name: v.name,
                post: v.major,
                mobile: v.peopleLX
              }
            })
          case '医疗机构':
            items = await commonApi.search('yj_mail_list_yl', { skip, limit })
            return items.map(v => {
              return {
                name: v.name,
                post: '',
                mobile: v.mobile
              }
            })
          case '安管人员':
            let safetyMaDic = await commonApi.dictionary('safety_ma')
            items = await commonApi.search('spe_safety_admins', { skip, limit })
            items = items.map(v => {
              return {
                name: v.name,
                post: v.safety_manager_category,
                mobile: v.tel
              }
            })
            return ReplaceDictionary(items, { post: safetyMaDic })
          case '特殊作业人员':
            let workTypeDic = await commonApi.dictionary('work_type')
            items = await commonApi.search('spe_special_worker')
            items = items.map(v => {
              return {
                name: v.name,
                post: v.job_title,
                mobile: v.tel
              }
            })
            return ReplaceDictionary(items, { post: workTypeDic })
          case '重大危险源负责人':
            items = await commonApi.search('security_risk_filing')
            return items.map(v => {
              return {
                name: v.responsible,
                post: v.responsibleDepart,
                mobile: v.responsibleMobile
              }
            })
          case '企业主要负责人':
            items = await commonApi.search('ent_enterprise_PersonnelInfo', {
              equalTo: { PersonnelClassification: '主要负责人' }
            })
            return items.map(v => {
              return {
                name: v.UserName,
                post: v.UserDuties,
                mobile: v.ContactNumber
              }
            })
          case '企业安全负责人':
            items = await commonApi.search('ent_enterprise_PersonnelInfo', {
              equalTo: { PersonnelClassification: '安全负责人' }
            })
            return items.map(v => {
              return {
                name: v.UserName,
                post: v.UserDuties,
                mobile: v.ContactNumber
              }
            })
          case '企业安全管理人员':
            items = await commonApi.search('ent_enterprise_PersonnelInfo', {
              equalTo: { PersonnelClassification: '安全管理人员' }
            })
            return items.map(v => {
              return {
                name: v.UserName,
                post: v.UserDuties,
                mobile: v.ContactNumber
              }
            })
          case '视频管理人员':
            items = await commonApi.search('measure_video', {
              notEqualTo: { AdminPersonnel: '' },
              exists: ['AdminPersonnel']
            })
            return items.map(v => {
              return {
                name: v.AdminPersonnel,
                post: v.AdminDepartment,
                mobile: v.AdminPersonnelMobile
              }
            })
          case '设备管理人员':
            items = await commonApi.search('measure_automation', {
              notEqualTo: { AdminPersonnel: '' },
              exists: ['AdminPersonnel']
            })
            return items.map(v => {
              return {
                name: v.AdminPersonnel,
                post: v.AdminDepartment,
                mobile: v.AdminPersonnelMobile
              }
            })
          default:
            return []
        }
      }
    }
  },
  methods: {
    onSearch() {
      this.search = false
      this.$refs.group.startPull()
    }
  }
}
</script>

<style scoped lang="stylus">
>>> .yt-collapse-content
  padding 0

>>> .yt-collapse-content
  .yt-cell
    background-color transparent

  .yt-cell-label
    padding 2px 0

>>> .badge
  font-size 12px
  line-height normal
  padding 1px 15px
  border-radius 20px
  color #fff
  margin-left 20px
  max-width 60%
  overflow hidden
  white-space nowrap
  text-overflow ellipsis

>>>.yt-form
  background-color white
</style>
